package com.ttbj.core.util;

import java.io.IOException;
import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

/**
 * zookeeper客户端操作
 * 
 * @author Daniel
 * @date 2017年1月9日 下午6:14:06
 * 
 */
public class ZookeeperUtil {

	public static void main(String[] args) throws IOException, KeeperException, InterruptedException {

		// String zkServer = "192.168.34.80:2181";
		String zkServer = "127.0.0.1:2181";
		String path = "/dubbo";
		zookeeperClientOperate(zkServer, path);
	}

	/**
	 * zookeeper客户端操作接口
	 * 
	 * @author Daniel
	 * @date 2017年1月9日 下午6:13:45
	 * 
	 * @param zkServer
	 * @param path
	 * @throws IOException
	 * @throws KeeperException
	 * @throws InterruptedException
	 */
	protected static void zookeeperClientOperate(String zkServer, String path) throws IOException, KeeperException, InterruptedException {
		ZooKeeper zk = new ZooKeeper(zkServer, 3000, new Watcher() {
			// 监控所有被触发的事件
			@Override
            public void process(WatchedEvent event) {
				System.out.println("已经触发了" + event.getType() + "事件！");
			}
		});
		// 创建一个目录节点
		// zk.create("/panguoyuan-dir", "panguoyuan-data".getBytes(),
		// Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		// 创建一个子目录节点
		// zk.create("/panguoyuan-dir/childone", "childone".getBytes(),
		// Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		// System.out.println(new String(zk.getData("/panguoyuan-dir", false,
		// null)));
		// 取出子目录节点列表

		List<String> dubbos = zk.getChildren(path, true);
		for (String dubbo : dubbos) {
			System.out.println(dubbo);
			String serviceName = "/dubbo/" + dubbo;

			List<String> facades = zk.getChildren(serviceName, true);
			for (String facade : facades) {
				List<String> details = zk.getChildren(serviceName + "/" + facade, true);
				if (!(details != null && details.size() > 0)) {
					zk.delete(serviceName + "/" + facade, -1);
				} else {
					for (String detail : details) {
						List<String> detailSubs = zk.getChildren(serviceName + "/" + facade + "/" + detail, true);
						if (!(detailSubs != null && detailSubs.size() > 0)) {
							zk.delete(serviceName + "/" + facade + "/" + detail, -1);
						} else {
							for (String sub : detailSubs) {
								System.out.println("打印：" + sub);
							}
						}

					}
				}
			}

		}

		// zk.delete("/dubbo/com.yinhu.order.api.RechargeFacade/consumers/consumer%3A%2F%2F192.168.35.28%2Fcom.yinhu.order.api.RechargeFacade%3Fapplication%3Dyinhu-order-node-consumer%26category%3Dconsumers%26check%3Dfalse%26cluster%3Dfailsafe%26dubbo%3D2.8.3%26group%3Dliuhlorder%26interface%3Dcom.yinhu.order.api.RechargeFacade%26methods%3DrechargeByUser%26pid%3D4640%26retries%3D1%26revision%3D1.0.0-SNAPSHOT%26side%3Dconsumer%26timeout%3D60000%26timestamp%3D1483602570190%26version%3D1.0.0",
		// -1);
		// 修改子目录节点数据
		// zk.setData("/panguoyuan-dir/childone", "modifyDataOne".getBytes(),
		// -1);
		// System.out.println("目录节点状态：[" + zk.exists("/panguoyuan-dir", true) +
		// "]");
		// 创建另外一个子目录节点
		// zk.create("/panguoyuan-dir/childtwo", "childtwo".getBytes(),
		// Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		// System.out.println(new String(zk.getData("/panguoyuan-dir/childtwo",
		// true, null)));
		// 删除子目录节点
		// zk.delete("/panguoyuan-dir/childtwo", -1);
		// zk.delete("/panguoyuan-dir/childone", -1);
		// 删除父目录节点

		// 关闭zk连接
		zk.close();
	}

}
